/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.mods.shooter.states.objects;

import java.util.LinkedList;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.scene.Geometry;
import etherblocks.engine.*;
import etherblocks.engine.objects.*;

/**
 *
 * @author Carl
 */
public class WeaponObject extends ModelObject{
    
    public WeaponObject(MainApplication mainApplication){
        super("/Models/pistol/skin.xml");
        this.mainApplication =  mainApplication;
        setLocalTranslation(-2.75f, -2.8f, 5);
        rotate(0, (float) (Math.PI / 1.9), 0);
        shotExplosion.setLocalTranslation(-1.4f, 1.55f, 0);
        attachChild(shotExplosion);
        LinkedList<Geometry> geometryChilds = JMonkeyUtil.getAllGeometryChilds(this);
        for(int i=0;i<geometryChilds.size();i++){
            Geometry geometryChild = geometryChilds.get(i);
            geometryChild.setName(NAME_CHILD_GEOMETRY);
        }
    }
    public static final String NAME_CHILD_GEOMETRY = "weapon_child_geometry";
    private static final String ANIMATION_NAME_SHOT = "shot";
    private MainApplication mainApplication;
    private ShotExplosionObject shotExplosion = new ShotExplosionObject();
    
    public void shoot(){
        playAnimation(ANIMATION_NAME_SHOT, 3);
        shotExplosion.playAnimation();
    }

    @Override
    public void onAnimCycleDone(AnimControl control, AnimChannel channel,final String animationName){
        super.onAnimCycleDone(control, channel, animationName);
        if(animationName.equals(ANIMATION_NAME_SHOT)){
            mainApplication.enqueueTask(new Runnable(){

                public void run(){
                    stopAndRewindAnimation();
                }
            });
        }
    }
}
